import * as drop from '@/api/drop'

const droplist = {
  state: {
    POLLUTANT_TYPE: [],
    POLLUTANT_SUB_TYPE: []
  },

  mutations: {
    SET_POLLUTANT_TYPE: (state, POLLUTANT_TYPE) => {
      state.POLLUTANT_TYPE = POLLUTANT_TYPE
    },
    SET_POLLUTANT_SUB_TYPE: (state, POLLUTANT_SUB_TYPE) => {
      state.POLLUTANT_SUB_TYPE.push(POLLUTANT_SUB_TYPE)
    },
  },

  actions: {
    //测试获取
    GetPOLLUTANT_TYPE({ commit }) {
      return new Promise((resolve, reject) => {
        if (droplist.state.POLLUTANT_TYPE.length != 0) {
          resolve(droplist.state.POLLUTANT_TYPE)
          return
        }
        drop.GetPOLLUTANT_TYPE().then(response => {
          if (response.code === '0000') {
            commit('SET_POLLUTANT_TYPE', response.data)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetPOLLUTANT_SUB_TYPE({ commit }, param) {
      return new Promise((resolve, reject) => {
        if (typeof droplist.state.POLLUTANT_SUB_TYPE.find(m => m.code === param.id) != 'undefined') {
          resolve(droplist.state.POLLUTANT_SUB_TYPE.find(m => m.code === param.id).data)
          return
        }
        drop.GetPOLLUTANT_SUB_TYPE(param).then(response => {
          const result = response.result
          if (response.code === 0) {
            const data = {
              data: result.data,
              code: param.id
            }
            commit('SET_POLLUTANT_SUB_TYPE', data)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default droplist
